package com.victorvieux.android.vlcmote;



import com.victorvieux.android.vlcmote.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class conf extends Activity {
	
	public static final String PREFS_NAME = "VLCMote_prefs";
	public static final int STATE_CONF_OK = 1;
	public static final int STATE_CONF_NOK = 2;
	
	private WifiManager wmw;
	private EditText TextIp;
	private EditText TextPort;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.conf);
        
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        int textColor = settings.getInt("textColor", 0xffff4500);
        TextView tv;
        
        tv = (TextView) findViewById(R.id.Title);
        tv.setTextColor(textColor);
        tv = (TextView) findViewById(R.id.TextBy);
        tv.setTextColor(textColor);
        tv = (TextView) findViewById(R.id.TextWifi);
        tv.setTextColor(textColor);
        tv = (TextView) findViewById(R.id.Textip);
        tv.setTextColor(textColor);
        
        ToggleButton wifiB = (ToggleButton) findViewById(R.id.ToggleWifi);
        
        ImageButton connectB = (ImageButton) findViewById(R.id.ImageConnect);
        TextIp = (EditText) findViewById(R.id.EditIp);
        TextPort = (EditText) findViewById(R.id.EditPort);
        
        wifiB.setOnClickListener(new wifi());
        connectB.setOnClickListener(new connect());
        TextIp.setText(settings.getString("ip", "0.0.0.0"));
        TextPort.setText(settings.getString("port", "8080")); 
        
        wmw = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        if (wmw.isWifiEnabled())
        	wifiB.setChecked(true);
        else
        	wifiB.setChecked(false);
        conf.this.setResult(STATE_CONF_NOK);
	}
    
    class  connect implements ImageButton.OnClickListener {
		public void onClick(View v) {
			ToggleButton wifiB = (ToggleButton) findViewById(R.id.ToggleWifi);
			if (wifiB.isChecked())
				conf.this.setResult(STATE_CONF_OK);
		         

		    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		    SharedPreferences.Editor editor = settings.edit();
		    editor.putString("ip", TextIp.getText().toString());
		    editor.putString("port", TextPort.getText().toString());
		    editor.commit();
		    conf.this.finish();
		}
    }
    
    class 	wifi implements CompoundButton.OnClickListener {
		public void onClick(View v) {
			ToggleButton wifiB = (ToggleButton) v;
			if (!wifiB.isChecked())
				wmw.setWifiEnabled(false);
			else {
				wmw.setWifiEnabled(true);
	        	conf.this.setResult(STATE_CONF_OK); 
				AlertDialog alert = new AlertDialog.Builder(conf.this).create();
				alert.setTitle(R.string.diagT);
				alert.setMessage(getText(R.string.diagM2));
				alert.setButton(getText(R.string.ConfCancel), new DialogInterface.OnClickListener() {
        	      public void onClick(DialogInterface dialog, int which) {
        	          return;
        	        } });

				alert.show();
			}
		}
    }
}
